<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\components\widgets\Datetimepicker;
Datetimepicker::widget(['timepicker' => 'true','datepicker'=>'false','format'=>'H:i','step'=>'5']);
?>
<div class="yx-praise-list-box yx-praise-box yx-radius2 zx-min-height" >
    <?php $form = ActiveForm::begin(['enableAjaxValidation' => true,'enableClientValidation' => false,'options'=>['class'=>'form-validate']]); ?>
    <table width="100%" class="table-form" >
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['type']; ?></th>
            <td style="display: inline-block;width: 206px"><?= $form->field($model, 'type', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>['1'=>'益智','2'=>'音乐','3'=>'形体'],'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['class_day']; ?></th>
            <td style="display: inline-block;width: 206px"><?= $form->field($model, 'class_day', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>['1'=>'星期一','2'=>'星期二','3'=>'星期三','4'=>'星期四','5'=>'星期五','6'=>'星期六','7'=>'星期天'],'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['star_time']; ?></th>
            <td><?= $form->field($model, 'star_time', ['template' => '{input}{error}{hint}'])->textInput(['class' => 'datetimepicker']) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['end_time']; ?></th>
            <td><?= $form->field($model, 'end_time', ['template' => '{input}{error}{hint}'])->textInput(['class' => 'datetimepicker']) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['price']; ?></th>
            <td><?= $form->field($model, 'price', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px','class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table"><?= $model->attributeLabels()['max_count']; ?></th>
            <td><?= $form->field($model, 'max_count', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px','class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th class="zx-goods-table">课程</th>
            <td>
                <table width="100%" id="add_tr">
                    <?php if(!empty($class)) { ?>
                       <?php foreach ($class as $k=>$v) { ?>
                            <tr class="tr">
                                <td width="50px">老师</td>
                                <td width="260px"><select name="teacher[]">
                                        <option>请选择老师</option>
                                        <?php foreach ($teacher as $kk=>$vv) { ?>
                                            <?php if($vv['id'] == $v['teacher']){ ?>
                                                <option value="<?= $vv['id']?>" selected="selected"><?= $vv['name']?></option>
                                            <?php }else{ ?>
                                                <option value="<?= $vv['id']?>"><?= $vv['name']?></option>
                                            <?php } ?>
                                        <?php } ?>

                                    </select></td>
                                <td width="50px">课程</td>
                                <td width="260px"><select  name="course[]">
                                        <option>请选择课程</option>
                                        <?php foreach ($course as $kk=>$vv) { ?>
                                            <?php if($vv['id'] == $v['course']){ ?>
                                                <option value="<?= $vv['id']?>" selected="selected"><?= $vv['name']?></option>
                                            <?php }else{ ?>
                                                <option value="<?= $vv['id']?>"><?= $vv['name']?></option>
                                            <?php } ?>
                                        <?php } ?>
                                    </select></td>
                                <td width="50px">教室</td>
                                <td width="260px"><select  name="classroom[]">
                                        <option>请选择教室</option>
                                        <?php foreach ($classroom as $kk=>$vv) { ?>
                                            <?php if($vv['id'] == $v['classroom']){ ?>
                                                <option value="<?= $vv['id']?>" selected="selected"><?= $vv['name']?></option>
                                            <?php }else{ ?>
                                                <option value="<?= $vv['id']?>"><?= $vv['name']?></option>
                                            <?php } ?>
                                        <?php } ?>
                                    </select></td>
                                <td>
                                   <?php if($k == 1) { ?>
                                    <a href="javascript:;" class="yx-batch-settlement yx-bg7ecef4 yx-ml10 yx-radius2" id="add_class" >新增</a>
                                   <?php }else{ ?>
                                       <a href="javascript:;" class="yx-batch-settlement yx-bgff0000 remove_class yx-ml10 yx-radius2" >移除</a>
                                   <?php } ?>
                                </td>
                            </tr>
                       <?php } ?>
                    <?php } else {?>
                    <tr class="tr">
                        <td width="50px">老师</td>
                        <td width="260px"><select name="teacher[]">
                                <option value="">请选择老师</option>
                                <?php foreach ($teacher as $tea) {?>
                                     <option value="<?=$tea->id?>"><?=$tea->name?></option>
                                <?php } ?>
                            </select></td>
                        <td width="50px">课程</td>
                        <td width="260px"><select  name="course[]"></select></td>
                        <td width="50px">教室</td>
                        <td width="260px"><select  name="classroom[]"></select></td>
                        <td><a href="javascript:;" class="yx-batch-settlement yx-bg7ecef4 yx-ml10 yx-radius2" id="add_class" >新增</a></td>
                    </tr>
                   <?php } ?>
                </table>

            </td>
        </tr>
    </table>
</div>
<div class="m_top_10 btn" style="width: 100%">
    <?= \yii\helpers\Html::submitButton('提交', ['class' => "button", 'id' => 'dosubmit']) ?>
</div>
<?php \yii\widgets\ActiveForm::end(); ?>
<?php
$js = <<<JS

    //根据type_id确定副课老师、课程、教室类型
    $('#artteaching-type').change(function(){
        var type_id = $('#artteaching-type').val();
        $.ajax({  
            url:'/artteaching-gettype',
            data:{  
                type_id : type_id
            },  
            type:'post',  
            cache:false,  
            dataType:'json',  
            success:function(data) {  
                // var teacher = data.teacher; 
                // var str1 = '<option>请选择老师</option>';
                // for(var i=0;i<teacher.length;i++){
                //     var data1 = teacher[i];
                //     //console.log(data1);return false;
                //     str1 += '<option value='+data1.id+'>'+data1.name+'</option>';
                // }
                // $('select[name="teacher[]"]').html(str1);
                
                var classroom = data.classroom;
                var str2 = '<option>请选择教室</option>';
                for(var j=0;j<classroom.length;j++){
                    var data2 = classroom[j];
                    //console.log(data1);return false;
                    str2 += '<option value='+data2.id+'>'+data2.name+'</option>';
                }
                $('select[name="classroom[]"]').html(str2);
               
                var course = data.course;
                var str3 = '<option>请选择课程</option>';
                for(var k=0;k<course.length;k++){
                    var data3 = course[k];
                    //console.log(data1);return false;
                    str3 += '<option value='+data3.id+'>'+data3.name+'</option>';
                }
                $('select[name="course[]"]').html(str3);   
            }
        });
    });
    
    //点击添加新副课
    $('#add_class').click(function(){
        var tr = $(this).parents('.tr');
        var addItem = tr.html().replace('新增','移除');
        var addItem = addItem.replace('id="add_class"','');
        var addItem = addItem.replace('yx-bg7ecef4','yx-bgff0000 remove_class');
        var str = '<tr>';
        str += addItem;
        str += '</tr>';
        $("#add_tr").append(str);
    });
    
    //点击移除副课
    $(document).on('click','.remove_class',function(){     
          $(this).parent().parent().remove();
    });  
    //点击提交 先判断#add_tr是否与class_count 相等
    // $('#dosubmit').click(function(){
    //     var count = $('#artteaching-class_count').val();
    //     var ids = $("#add_tr tr").length;
    //     if(!(count == ids)){
    //         alert('添加课次数量应与填写的课次数相同');
    //         return false;
    //     }
    // });
    
JS;
$this->registerJS($js);
?>

